﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Carbon_Tracker
{
    public partial class frmPriority : Form
    {
        Formulas.PriorityReport report;
        public frmPriority()
        {
            InitializeComponent();
            this.Cursor = Cursors.WaitCursor;
            report = new Formulas.PriorityReport(DatabaseInterface.Connection.Singleton);
        }

        private void dtpPriorityDate_ValueChanged(object sender, EventArgs e)
        {
            UpdateDataGrid();
        }
        private void UpdateDataGrid()
        {
            DateTime date = dtpPriorityDate.Value;
            dataGridView1.DataSource = this.report.GetPriority(date, (BaseUnits.Grid)cmbGrid.SelectedItem);
            dataGridView1.Sort(dataGridView1.Columns["Priority"], ListSortDirection.Ascending);
        }

        private void frmPriority_Load(object sender, EventArgs e)
        {
            try
            {
                BaseUnits.Builders.GridBuilder gBuilder = new BaseUnits.Builders.GridBuilder(DatabaseInterface.Connection.Singleton);
                IList<BaseUnits.IPowerGrid> grids = gBuilder.GetGrids();
                foreach (BaseUnits.IPowerGrid g in grids)
                {
                    cmbGrid.Items.Add(g);
                }
                cmbGrid.SelectedItem = cmbGrid.Items[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            this.Cursor = Cursors.Default;
        }

        private void cmbGrid_SelectedIndexChanged(object sender, EventArgs e)
        {
            UpdateDataGrid();
        }

        private void dataGridView1_ColumnMinimumWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            
        }

        private void dataGridView1_ColumnWidthChanged(object sender, DataGridViewColumnEventArgs e)
        {
            //int gridWidth = dataGridView1.Width;
            //int colWidth = gridWidth;
            //DataGridViewColumn col = dataGridView1.Columns[2];
            //foreach (DataGridViewColumn c in dataGridView1.Columns)
            //{
            //    if ((c != col)&&(c.Visible))
            //    {
            //        colWidth -= c.Width;
            //    }
            //}
            //col.Width = colWidth - dataGridView1.RowHeadersWidth;
            //dataGridView1
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (chkOnlyThisDate.Checked)
                {
                }
                else
                {
                    this.report.UpdatePriority((Formulas.ReportData.PriorityDataTable)dataGridView1.DataSource);
                }
                MessageBox.Show("Priority table updated succesfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
    }
}
